var uid = getParameter("uid");
var day = getParameter("Day");
var now = new Date();
var arg = 1;
var code=getParameter("code");
$(function() {
	if(uid){
	charts(now);
	}else{
		getToken(code);
	}
});
function getToken(code_){
	var urlStr = serverPath + "/server/doctor/diabetes/login.do";
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"",'+
	    '	   "uid":"",'+
	    '	   "method":"taipinglogin",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	    '	      {'+
	    '	         "code":"'+code_+'"'+
	    '	      }'+
	    '	   ]'+
	    '	}';
	
	UseAjax(urlStr, jsonStr, document.cookie, getUid, '1');

};

function getUid(data) {
	if(data.resultcode=='0'){
		uid = data.uid;
		charts(now);
	}else{
		alert(data.resultmsg);	
	}
}
function charts(now) {
	if (day == 30) {
		arg = 31;
	} else if (day == 7) {
		arg = 7;
	}
	var urlStr = serverPath + "/app/doctor/diabetes/sportView.do";
/*	var jsonStr = '{method:"showSportTable",UId:"' + UId + '",date:"'
			+ now.format("yyyy-MM-dd") + '",day:"' + day + '",befdate:"'
			+ getLastDay(now, arg).format("yyyy-MM-dd") + '",aftdate:"'
			+ getNextDay(now, arg).format("yyyy-MM-dd") + '"}';*/
	
	
	
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"",'+
	    '	   "uid":"'+uid+'",'+
	    '	   "method":"showSportTable",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	    '	      {'+
	    '	         "uid":"'+uid+'"'+','+
	    '            "date":"'+now.format("yyyy-MM-dd")+'",'+
	    '            "day":"'+day+'",'+
	    '            "befdate":"'+getLastDay(now, arg).format("yyyy-MM-dd")+'",'+
	    '            "aftdate":"'+getLastDay(now, 1).format("yyyy-MM-dd")+'",'+	    
	    '	      }'+
	    '	   ]'+
	    '	}';
	UseAjax(urlStr, jsonStr, document.cookie, rt, '1');
}
function rt(data) {
	var records = data.resultlist[0].records;
	var html="";
	html = '<table class="bordered"><thead> '+ 
      '<tr><th>日期</th><th>步数</th><th>卡路里</th> '+ 
       '</tr></thead>';
	if (records) {
		for (var int = 0; int < records.length; int++) {
			var wi = records[int];// .substring(0,10)
	         html+='<tr><td>'+wi.dates.substring(5, 10)+
	           '</td><td>'+isnull(wi.stepnum)+'</td><td>'
	            +isnull(wi.calorie)+
                '</td></tr>';
		}
	}
	$("#container").html(html);
	//设置表格颜色
	$("tr:odd").each(function(index) {
		$(this).css("background-color","#e0ecfb");
	});
	$("tr:first").css("color","#ffffff");
}

$(function() {
	// swipe 的dome
	$("#container").swipe(
			{
				swipeLeft : function(event, direction, distance, duration,
						fingerCount) {
				new Date()
				var temp = getNextDay(now, arg);
				if(temp<new Date()){
					now = temp;
					charts( now);
				}
				},
				swipeRight : function(event, direction, distance, duration,
						fingerCount) {
					var temp = getLastDay(now, arg);
					if(temp<new Date()){
						now = temp;
						charts( now);
					}
				}
			});

});
function isnull(value) {
	if(value == null || value == "" || value == undefined){
		return "";
	}else{
		return value;
	}
}